<template>
  <div>
    <el-row>
      <el-col :span="form.type==2?10:24">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="活动昵称:" prop="title">
        <el-input v-model="form.title" style="width:30%" placeholder="请输入活动昵称" />
      </el-form-item>

      <el-form-item label="封面:" prop="thumb">
        <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100"/>
      </el-form-item>
      <el-form-item label="轮播:" prop="banner">
        <upload-pic :thumb="form.banner" @update:thumb="form.banner=$event" width="100"/>
      </el-form-item>
      <el-form-item label="活动类型:" prop="type">
        <el-select v-model="form.type" style="width:30%" placeholder="请选择活动类型">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="指定商品:" prop="goods_ids" v-if="form.type==1">
        <el-select v-model="form.goods_ids" style="width:30%" clearable multiple placeholder="请选择">
          <el-option
            v-for="item in goods_list"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="路径:" prop="path" v-if="form.type==2">
        <el-input v-model="form.path" style="width:80%" placeholder="请输入路径链接" />
      </el-form-item>
      <el-form-item label="详情:" prop="content" v-if="show_content||form.type==0">
        <rich-text ref="myQuillEditor" :content="form.content" />
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input-number v-model="form.sort" style="width:30%" placeholder="请输入排序" />
        <!-- <el-input v-model="form.sort" style="width:30%" placeholder="请输入排序" type="number" /> -->
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-close" circle @click="bk()" />
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="onSubmit('form')"
        />
      </el-form-item>
    </el-form>
    </el-col>
      <el-col :span="14" v-if="form.type==2">
        <div class="menu_title">选择跳转地址:</div>
        <url-address @selectMenu="selectMenu"></url-address>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import richText from '../../../components/rich_text/rich_text'
import urlAddress from '../../../components/url_address/urlAddress'
export default {
  name: 'ActivityForm',
  components: { richText,urlAddress },
  data() {
    return {
      id: this.$store.state.editId,
      options: [
        { id: 0, title: '文本' },
        { id: 1, title: '商品' }
      ],
      goods_list: [],
      banner: '',
      show_content: true,
      thumb: '',
      form: {},
      rules: {
        title: [
          { required: true, message: '请输入活动昵称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择活动类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.init_into()
  },
  methods: {
    selectMenu (item) {
      // item.status === 1
      //   ? (this.form.status = true)
      //   : (this.form.status = false);
        this.thumb = this.thumb ? this.thumb : item.thumb;
        this.form.thumb = this.form.thumb ? this.form.thumb : item.thumb;
        this.form.title = this.form.title ? this.form.title : item.title
        this.form.path = item.path
    },
    // 图片上传完成
    handleAvatarSuccess_banner(res, file) {
      this.banner = file.response.data.path
      this.form.banner = file.response.data.path
    },

    // 提交
    onSubmit(formName) {
      const form = this.form
      if(form.type==0)
      form.content = this.$refs.myQuillEditor.editor_content
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Activity/save', form, 'POST').then(e => {})
          this.$store.state.edit = ''

        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$store.state.edit = ''
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Activity/info?id=' + this.id, 'POST').then(e => {
        this.goods_list = e.goods_list
        this.form = e.info.tpl
        this.form.status === 1 || this.form.status === '' ? this.form.status = true : this.form.status = false
        this.options=e.type_arr
        for(let i in e.type_arr){
          this.options[i]={ id: Number(i), title: e.type_arr[i] }
        }
        if(this.form.type==0)
          this.$refs.myQuillEditor.editor_content = this.form.content
        else
          this.show_content=false
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}
</style>
